package com.wx.day03.Work.Dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.wx.day03.Work.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository

public class StuentDaoimpl implements StudentDao {
    @Autowired
    private DataSource dataSource;

    @Override
    public List<Student> queryAll() throws Exception {
        Connection connection = dataSource.getConnection();
        String sql = "select id, name, gender, age from students";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        List<Student> studentList = new ArrayList<>();
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Student student = new Student();
            student.setId(resultSet.getInt("id"));
            student.setName(resultSet.getString("name"));
            student.setGender(resultSet.getString("gender"));
            student.setAge(resultSet.getInt("age"));
            studentList.add(student);
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return studentList;
    }
}
